class StorageService {
    constructor() {
        this.localData = {};
        this.sessionData = {};
        this.loadFromLocalStorage();
        this.loadFromSessionStorage();
    }

    // 从本地存储加载数据
    loadFromLocalStorage() {
        for (const key in localStorage) {
            if (localStorage.hasOwnProperty(key)) {
                this.localData[key] = localStorage.getItem(key);
            }
        }
    }

    // 从会话存储加载数据
    loadFromSessionStorage() {
        for (const key in sessionStorage) {
            if (sessionStorage.hasOwnProperty(key)) {
                this.sessionData[key] = sessionStorage.getItem(key);
            }
        }
    }

    // 将数据保存到本地存储，key为字符串类型，value为字符串类型（同样可按需调整类型）
    saveToLocalStorage(key, value) {
        localStorage.setItem(key, value);
        this.localData[key] = value;
    }

    // 将数据保存到会话存储，key为字符串类型，value为字符串类型
    saveToSessionStorage(key, value) {
        sessionStorage.setItem(key, value);
        this.sessionData[key] = value;
    }

    // 从本地存储中移除数据，key为字符串类型
    removeFromLocalStorage(key) {
        localStorage.removeItem(key);
        delete this.localData[key];
    }

    // 从会话存储中移除数据，key为字符串类型
    removeFromSessionStorage(key) {
        sessionStorage.removeItem(key);
        delete this.sessionData[key];
    }

    // 清空本地存储
    clearLocalStorage() {
        localStorage.clear();
        this.localData = {};
    }

    // 清空会话存储
    clearSessionStorage() {
        sessionStorage.clear();
        this.sessionData = {};
    }
}

const storageService = new StorageService();
export default storageService;